#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mythread.h"
#include <QThreadPool>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //创建任务类对象
    Generate* generate = new Generate;
    BubbleSort* bubble_sort = new BubbleSort;
    QuickSort* quick_sort = new QuickSort;
    //启动子线程
    connect(this , &MainWindow::starting, generate , &Generate::recvNum);
    connect(ui->start, &QPushButton::clicked , this ,[=](){
        emit starting(10000);
        QThreadPool::globalInstance()->start(generate);
    });
    connect(generate,&Generate::sendArray,bubble_sort,&BubbleSort::recvArray);
    connect(generate,&Generate::sendArray,quick_sort,&QuickSort::recvArray);
    //接受子线程发送的数据
    connect(generate , &Generate::sendArray , this , [=](QVector<int> list){
       QThreadPool::globalInstance()->start(bubble_sort);
       QThreadPool::globalInstance()->start(quick_sort);
       for (int i=0;i<list.size();++i){
           ui->rand_list->addItem(QString::number(list.at(i)));
       }
    });
    connect(bubble_sort , &BubbleSort::Finish_Array , this , [=](QVector<int> list){
       for (int i=0;i<list.size();++i){
           ui->bubbel_list->addItem(QString::number(list.at(i)));
       }
    });
    connect(quick_sort , &QuickSort::Finish_Array , this , [=](QVector<int> list){
       for (int i=0;i<list.size();++i){
           ui->quick_list->addItem(QString::number(list.at(i)));
       }
    });

}

MainWindow::~MainWindow()
{
    delete ui;
}

